<template>
  <el-dialog @close="shkczlCancel('shkczlRuleForm')" :close-on-click-modal="false" title="上传勘场资料"
    :visible.sync="shkczlVisible" width="600px">
    <div class="scroll-view">
      <el-form :model="shkczlRuleForm" :rules="shkczlRules" ref="shkczlRuleForm" label-position="top">
        <el-form-item label="资料文件(.zip,.ppt,.xls,.pdf,.doc,.docx,.xlsx)" prop="survey_data">
          <div class="file-box">
            <el-input style="position: absolute; z-index: 0; top: -999999px"
              v-model="shkczlRuleForm.survey_data.length"></el-input>
            <el-upload accept=".zip,.ppt,.xls,.pdf,.doc,.docx,.xlsx" :show-file-list="true" :auto-upload="false" action="#"
              :file-list="shkczlRuleForm.survey_data" :on-change="(file, fileList) => {
                shkczlFileChange(file, fileList, 'survey_data');
              }" :on-remove="(file, fileList) => {
                shkczlFileRemove(file, fileList, 'survey_data');
              }">
              <el-button size="small" icon="el-icon-plus">点击上传</el-button>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input :rows="4" class="input-box" v-model="shkczlRuleForm.remark" type="textarea"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer">
      <el-button @click="shkczlCancel('shkczlRuleForm')">关闭</el-button>
      <el-button type="primary" @click="shkczlSubmit('shkczlRuleForm')" :disabled="diaLoading" :loading="diaLoading">确
        定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import projectApi from "@/api/projectApi";
import companyApi from "@/api/companyApi";
import adminApi from "@/api/adminApi";
import { getUserInfo } from "@/utils/auth";
import foremanApi from "@/api/foremanApi";

export default {
  name: "",
  props: {
    addDialogVisible: {
      type: Boolean,
      default: false,
      required: false,
    },
    project_id: {
      required: false,
    },
  },
  data() {
    var survey_data = (rule, value, callback) => {
      if (!value.length) {
        callback(new Error("请上传勘场资料文件"));
      } else {
        callback();
      }
    };
    return {
      shkczlRuleForm: {
        remark: "",
        survey_data: [],
      },
      shkczlRules: {
        survey_data: [
          { required: true, validator: survey_data, trigger: "change" },
        ],
      },
      shkczlVisible: false,
      diaLoading: false,
    };
  },
  watch: {
    addDialogVisible(val1, val2) {
      this.shkczlVisible = this.addDialogVisible;
    },
  },
  computed: {},
  mounted() { },
  methods: {
    // 上传勘场资料
    shkczlSubmit(formName) {
      let shkczlRuleForm = this.shkczlRuleForm;
      console.log(shkczlRuleForm, "shkczlRuleForm");
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let formdata = new FormData();
          formdata.append(
            "data",
            JSON.stringify({
              remark: shkczlRuleForm.remark,
              project_id: this.project_id,
            })
          );
          shkczlRuleForm.survey_data.forEach((item) => {
            formdata.append("survey_data[]", item.raw);
          });
          this.diaLoading = true;
          projectApi
            .uploadSurveyData(formdata)
            .then((res) => {
              console.log(res, "上传勘场资料");
              this.diaLoading = false;
              if (res.code == 200) {
                this.$emit("onSuccess", true);
              }
              this.$message({
                type: res.code == 200 ? "success" : "warning",
                message: res.code == 200 ? "处理成功" : res.message,
              });
            })
            .catch((err) => {
              this.diaLoading = false;
              this.$message({
                type: "warning",
                message: err.message,
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    shkczlCancel(formName) {
      this.shkczlRuleForm = {
        survey_data: [],
        remark: "",
      };
      this.$nextTick(() => {
        this.$refs[formName].clearValidate();
        this.$emit("onClose", true);
      });
    },
    shkczlFileChange(file, list, type) {
      console.log(file, list, type);
      let shkczlRuleForm = this.shkczlRuleForm;
      shkczlRuleForm[type] = [file];
      this.shkczlRuleForm = shkczlRuleForm;
    },
    shkczlFileRemove(file, list, type) {
      console.log(file, list, type);
      let shkczlRuleForm = this.shkczlRuleForm;
      shkczlRuleForm[type] = list;
      this.shkczlRuleForm = shkczlRuleForm;
    },
  },
};

</script>
<style lang="scss" scoped>
@import "./index.scss";
</style>